package com.jie.other.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.primarySurface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.jie.commonlib.support.eventbus.Event
import com.jie.commonlib.support.eventbus.EventBusManager
import com.jie.commonlib.support.eventbus.EventConstant
import com.jie.other.R
import com.jie.other.compose.data.Message
import com.jie.other.compose.data.SampleData
import com.jie.other.ui.theme.OtherMaterialTheme
import com.jie.other.ui.widget.CommonTopAppBar
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Compose学习第一课
 */
class Study01Activity : ComponentActivity() {

    private val isFormApp = MutableLiveData(false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        EventBusManager.getInstance().register(this)
        setContent{
            val fromApp = remember { isFormApp }
            OtherMaterialTheme(
                isNewColor = fromApp.value?:false
            ) {
                StudyO1Page(
                    backClick = {
                        finish()
                    }
                )
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusManager.getInstance().unRegister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEvent(event: Event<Boolean>){
        LogUtils.i("接收到信息：${event}")
        if (event.code == EventConstant.CODE_FROM_APP){
            isFormApp.value = event.data
        }
    }
}

@Preview
@Composable
fun StudyO1Page(backClick : () -> Unit = {}) {
    Scaffold(
        topBar = {
            CommonTopAppBar(
                title = stringResource(id = R.string.other_compose_study_01),
                backClick = backClick
            )
        },
        content = {
            Surface(
                modifier = Modifier
                    .padding(top = it.calculateTopPadding())
                    .fillMaxSize()
            ) {
                MessageList(messages = SampleData.conversationSample)
            }
        }
    )
}

@Composable
fun MessageList(messages: List<Message>) {
    LazyColumn(content = {
        items(messages){
            MessageCard(message = it)
        }
    })
}

@Composable
fun MessageCard(message: Message) {
    Row(modifier = Modifier.padding(8.dp)) {
        Image(
            painter = painterResource(id = R.drawable.profile_picture),
            contentDescription = "",
            modifier = Modifier
                .size(40.dp)
                .clip(CircleShape)
                .border(1.5.dp, colorResource(id = R.color.colorPrimary), CircleShape)
        )
        Spacer(modifier = Modifier.width(8.dp))
        var isExpend by remember {
            mutableStateOf(false)
        }
        val surfaceColor by animateColorAsState(
            if (isExpend) {
                MaterialTheme.colors.primarySurface
            } else {
                MaterialTheme.colors.surface
            },
            label = ""
        )
        Column(modifier = Modifier.clickable {
            isExpend = !isExpend
        }) {
            Text(
                text = message.author,
                color = colorResource(id = R.color.colorPrimary),
                style = MaterialTheme.typography.subtitle2,
            )
            Spacer(modifier = Modifier.height(4.dp))
            Surface(
                shape = MaterialTheme.shapes.medium,
                elevation = 1.dp,
                color = surfaceColor,
                modifier = Modifier
                    .animateContentSize()
                    .padding(1.dp)
            ) {
                Text(
                    text = message.body,
                    modifier = Modifier.padding(10.dp),
                    maxLines = if (isExpend) Int.MAX_VALUE else 1,
                    style = MaterialTheme.typography.body2,
                )
            }
        }
    }
}